package com.smyh.UIM.device;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam1;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.cache.UIM.device.DeviceOptionFromCache;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.server.ToastShowMsgThread;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceSyncOptionFm extends Fragment {

    /**
     * 指示器参数 1组件
     */
    Button sendCmdBtn;
    /**
     * 指示器操作按钮
     */
    private Button shutUpOperBtn,resetOperBtn,defaultParamBtn,readyParamBtn;

    public Handler handler;
    Long faultWaveAbsoluteTimes;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_cmd_option,container,false);

        initView(view);
        initEvent();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        return view;
    }

    private void initEvent() {
        sendCmdBtn.setOnClickListener(readBtnListener);

        shutUpOperBtn.setOnClickListener(readBtnListener);
        resetOperBtn.setOnClickListener(readBtnListener);
        defaultParamBtn.setOnClickListener(readBtnListener);
        readyParamBtn.setOnClickListener(readBtnListener);
    }
    EditText subCmd01,subCmd02,netAddr01,netAddr02,groupAddr,linePhaseOff;
    private void initView(View view){
        subCmd01 = (EditText) view.findViewById(R.id.subCmd01);
        subCmd02 = (EditText) view.findViewById(R.id.subCmd02);
        netAddr01 = (EditText) view.findViewById(R.id.netAddr01);
        netAddr02 = (EditText) view.findViewById(R.id.netAddr02);
        groupAddr = (EditText) view.findViewById(R.id.groupAddr);
        linePhaseOff = (EditText) view.findViewById(R.id.linePhaseOff);

        DeviceOptionFromCache.subCmd01 = subCmd01;
        DeviceOptionFromCache.subCmd02 = subCmd02;
        DeviceOptionFromCache.netAddr01 = netAddr01;
        DeviceOptionFromCache.netAddr02 = netAddr02;
        DeviceOptionFromCache.groupAddr = groupAddr;
        DeviceOptionFromCache.linePhaseOff = linePhaseOff;

        sendCmdBtn = (Button) view.findViewById(R.id.sendCmdBtn);

        shutUpOperBtn = (Button)view.findViewById(R.id.shutUpOperBtn);
        resetOperBtn = (Button)view.findViewById(R.id.resetOperBtn);
        defaultParamBtn = (Button)view.findViewById(R.id.defaultParam);
        readyParamBtn = (Button)view.findViewById(R.id.readyParam);
    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam1 obj =  DeviceParamInfoCache.getDeviceParamMap1().get(MainCahce.chooseDeviceAddr);
//            Toast.makeText(MainActivity.context, "参数一读取成功...", Toast.LENGTH_SHORT).show();
        }
    };
    public static void freshDeviceParam1Model() {
        handler1.sendMessage(new Message());
    }

    private View.OnClickListener readBtnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(MinaClientThread.session != null && MinaClientThread.session.isConnected()){
                switch (v.getId()){
                    case R.id.sendCmdBtn:{

                        byte[] netAddrBytes = new byte[4];
                        netAddrBytes[0] = HexStrUtil.hexStr2Bytes(netAddr01.getText().toString())[0];
                        netAddrBytes[1] = HexStrUtil.hexStr2Bytes(netAddr02.getText().toString())[0];
                        netAddrBytes[2] = HexStrUtil.hexStr2Bytes(groupAddr.getText().toString())[0];
                        netAddrBytes[3] = HexStrUtil.hexStr2Bytes(linePhaseOff.getText().toString())[0];

                        byte cmd1 = HexStrUtil.hexStr2Bytes(subCmd01.getText().toString())[0];;
                        byte cmd2 = HexStrUtil.hexStr2Bytes(subCmd02.getText().toString())[0];;

                        IoBuffer controlByType = CMDUtil.controlByTypeSelfCmd(netAddrBytes,cmd1,cmd2);
                        MinaClientThread.session.write(controlByType);

                        if("82".equals(subCmd01.getText().toString())){
                            faultWaveAbsoluteTimes = System.currentTimeMillis();
                        }

                        Toast.makeText(getContext(),"同步指令下发！",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    case R.id.shutUpOperBtn:{

                        byte[] netAddrBytes = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                        byte subCMD = 1;
                        byte subCMD2 = 0;
                        IoBuffer buf = CMDUtil.controlByType(netAddrBytes, subCMD,subCMD2);
                        MinaClientThread.session.write(buf);

                        Toast.makeText(getContext(),"下发控制短路指令!",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    case R.id.resetOperBtn:{

                        byte[] netAddrBytes = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                        byte subCMD = 3;
                        byte subCMD2 = 0;
                        IoBuffer buf = CMDUtil.controlByType(netAddrBytes, subCMD,subCMD2);
                        MinaClientThread.session.write(buf);

                        Toast.makeText(getContext(),"下发复归指令!",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    case R.id.defaultParam:{

                        byte[] netAddrBytes = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                        byte subCMD = 0xb;
                        byte subCMD2 = 0;
                        IoBuffer buf = CMDUtil.controlByType(netAddrBytes, subCMD,subCMD2);
                        MinaClientThread.session.write(buf);

                        Toast.makeText(getContext(),"设置默认参数!",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    case R.id.readyParam:{

                        byte[] netAddrBytes = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                        byte subCMD = 0xb;
                        byte subCMD2 = 1;
                        IoBuffer buf = CMDUtil.controlByType(netAddrBytes, subCMD,subCMD2);
                        MinaClientThread.session.write(buf);

                        Toast.makeText(getContext(),"设置备用参数!",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    default:
                        break;
                }
            }else {

                new Thread(new ToastShowMsgThread("通讯异常！请检查")).start();
            }
        }
    };

}
